//
//  LYTabBar.m
//  artist
//
//  Created by Jay_Apple on 16/2/29.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYTabBar.h"
#import "UIImage+Image.h"
#import "LYPublishView.h"
#import "LYPublicTextViewController.h"

@interface LYTabBar()
/** 发布按钮 */
@property (nonatomic, weak) UIButton *publishButton;
//分割线
@property (nonatomic, weak) UIView *seperator;
//凸起
@property (nonatomic, weak) UIImageView *shapeImageView;
@end


@implementation LYTabBar


- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {

        //添加分割线
        UIView *seperator = [[UIView alloc] init];
        seperator.backgroundColor = [UIColor colorWithHexString:LYTabBarSeperatorColor alpha:1.0];

        //添加凸起图片
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"菜单栏扇形"]];
        [imageView sizeToFit];
        
        // 添加发布按钮
        UIButton *publishButton = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [self addSubview:seperator];
        [self addSubview:publishButton];
        [self addSubview:imageView];
        self.publishButton = publishButton;
        self.seperator = seperator;
        self.shapeImageView = imageView;
        
        [self setUpPublishBtn];
        
        //注册通知
        [self setUpThemeNotification];
        
    }
    return self;
}

- (void)dealloc{

    [self resignThemeNotification];
}

- (void)upDateTheme{

    [self setUpPublishBtn];
}

- (void)setUpPublishBtn{
    
    [self.publishButton setBackgroundImage:[[LYDataSourceManager sharedManager] getPublishBtnImage] forState:UIControlStateNormal];
    [self.publishButton setBackgroundImage:[[LYDataSourceManager sharedManager] getPublishBtnHighlightImage] forState:UIControlStateHighlighted];
    [self.publishButton addTarget:self action:@selector(publishClick) forControlEvents:UIControlEventTouchUpInside];
    self.publishButton.My_Size = self.publishButton.currentBackgroundImage.size;

}

/**
 *  大按钮点击
 */
- (void)publishClick
{
    
    [[VVYCheckStatus sharedInstance] checkIsLoginBlock:^(LYUserInfoModel *model) {
        if ([LYDataSourceManager sharedManager].type == roleTypeStu) {
            
            LYPublicPictureViewController *public = [[LYPublicPictureViewController alloc] init];
            [self puchToViewController:public];
            
            
        }else{
            [LYPublishView showWithBlock:^(publishType type) {
                
                
                
            }];
            
        }

    }];
    

}


- (void)puchToViewController:(UIViewController*)viewController{
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    LYTabBarController *tab = (LYTabBarController *)window.rootViewController;
    UINavigationController *nav =  tab.selectedViewController;
    [nav pushViewController:viewController animated:YES];
    
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    // 标记按钮是否已经添加过监听器
    static BOOL added = NO;
    
    CGFloat width = self.My_Width;
    CGFloat height = self.My_Height;
    
    // 设置发布按钮的frame
    
    CGFloat CenterX = width * 0.5;
    CGFloat CenterY = self.My_Height - self.publishButton.My_Size.height * 0.5 - 2;
    
    self.publishButton.center = CGPointMake(CenterX, CenterY);
    
    //设置分割线的frame
    self.seperator.frame = CGRectMake(0, 0, self.My_Width, 0.5);
    
    //设置凸起线的frame
    self.shapeImageView.My_CenterX = width * 0.5;
    self.shapeImageView.My_CenterY = -self.shapeImageView.My_Height * 0.5 + 1;
    
    // 设置其他UITabBarButton的frame
    CGFloat buttonY = 0;
    CGFloat buttonW = width / 5;
    CGFloat buttonH = height;
    NSInteger index = 0;
    for (UIControl *button in self.subviews) {
        if (![button isKindOfClass:[UIControl class]] || button == self.publishButton) continue;
        
        // 计算按钮的x值
        CGFloat buttonX = buttonW * ((index > 1)?(index + 1):index);
        button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
        
        // 增加索引
        index++;
    }
    
    added = YES;
}

@end
